package com.cloud.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.common.constants.GlobalConstants;
import com.cloud.common.enums.ProjectStatus;
import com.cloud.exception.CustomException;
import com.cloud.system.entity.SysMonitor;
import com.cloud.system.mapper.SysMonitorMapper;
import com.cloud.system.service.ISysMonitorService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务监控配置
 *
 * @author yzj
 * @since 2022-10-25
 */
@Service
public class SysMonitorServiceImpl extends ServiceImpl<SysMonitorMapper, SysMonitor> implements ISysMonitorService {

    @Resource
    private SysMonitorMapper sysMonitorMapper;

    /**
     * 监控配置分页
     *
     * @author yzj
     */
    @Override
    public List<SysMonitor> selectSysMonitorList(SysMonitor sysMonitor) {
        return sysMonitorMapper.selectSysMonitorList(sysMonitor);
    }

    /**
     * 查询项目应用服务列表
     *
     * @return
     */
    @Override
    public List<SysMonitor> selectProjectMonitorList() {
        return sysMonitorMapper.selectProjectMonitorList(ProjectStatus.APPLIED.getCode());
    }

    /**
     * 监控配置新增
     *
     * @author yzj
     */
    @Override
    public int insertObject(SysMonitor sysMonitor) {
        int rows = sysMonitorMapper.checkServiceNameUnique(sysMonitor.getServiceName());
        if (rows > 0) {
            throw new CustomException("操作失败:服务名称已存在");
        }
        sysMonitor.setServiceUrl(sysMonitor.getModulesName() + "/" + GlobalConstants.MONITOR_SERVICE_URL);
        sysMonitor.setRedisUrl(sysMonitor.getModulesName() + "/" + GlobalConstants.MONITOR_REDIS_URL);
        sysMonitor.setDruidUrl(sysMonitor.getModulesName() + "/" + GlobalConstants.MONITOR_DRUID_URL);
        return sysMonitorMapper.insert(sysMonitor);
    }

    /**
     * 监控配置修改
     *
     * @author yzj
     */
    @Override
    public int updateObject(SysMonitor sysMonitor) {
        int rows = sysMonitorMapper.checkServiceNameUnique(sysMonitor.getServiceName());
        if (rows > 0) {
            throw new CustomException("操作失败:服务名称已存在");
        }
        sysMonitor.setServiceUrl(sysMonitor.getModulesName() + "/" + GlobalConstants.MONITOR_SERVICE_URL);
        sysMonitor.setRedisUrl(sysMonitor.getModulesName() + "/" + GlobalConstants.MONITOR_REDIS_URL);
        sysMonitor.setDruidUrl(sysMonitor.getModulesName() + "/" + GlobalConstants.MONITOR_DRUID_URL);
        return sysMonitorMapper.updateById(sysMonitor);
    }

    /**
     * 根据主键修改服务状态
     *
     * @param status
     * @param monitorId
     * @return
     */
    @Override
    public int updateServiceStatus(String status, Long monitorId) {
        return sysMonitorMapper.updateServiceStatus(status, monitorId);
    }

    /**
     * 根据主键查询配置信息
     *
     * @param monitorId
     * @return
     */
    @Override
    public SysMonitor selectMonitorinfo(Long monitorId) {
        return sysMonitorMapper.selectMonitorinfo(monitorId);
    }

}
